/**
 * 新建报表相关JS
 */

$(function () {
    $.ajax({
        url:basePath + "/rptTools/saveRpt/queryDBCfg",
        type:'POST',
        dataType:'json',
        async: false,
        success:function(data){
            $.each(data.dbCfgs, function (index, obj) {
                var dbId = obj.sid;
                var dbName = obj.dbName;
                $("#dbSource").append("<option value='" + dbId + "'>" + dbName + "</option>");
            });

            $("#accessIn").append("<option value=''>请选择</option>");
            $.each(data.accessIns, function (index, obj) {
                var sysCode = obj.sysCode;
                var sysName = obj.sysName;
                $("#accessIn").append("<option value='" + sysCode + "'>" + sysName + "</option>");
            })
        }
    })
});

var addRpt = {
    /**
     * 上传报表表头文件
     */
    uploadeRptHeader : function() {
        var rptHeader = $("#rptHeaderInput").val();
        var rptName = $("#rptName").val();

        if(rptName == '') {
            alert("请输入报表名称");
            return;
        }
        if(rptHeader == '') {
            alert("请选择报表表头文件");
            return;
        }
        this.showMask();
        $("#rptForm").ajaxSubmit({
            url: basePath + "/rptTools/saveRpt/parseRptHeader",
            type: 'POST',
            dataType: "json",
            success: function (data) {
                $("#mask").hide();
                var status = data.status;
                var msg= data.msg;
                if (status == '0') {
                    alert(msg);
                } else {
                    alert("文件上传成功");
                    var rptHeader = data.rptHeader;
                    $("#rptHeaderJson").val(rptHeader);
                    $("#downRptCfg").show();
                }
            },
            error: function (data) {
                alert(data);
            }
        });
    },

    /**
     * 上传报表配置文件
     */
    uploadRptCfg : function () {
        var rptHeaderJson = $("#rptHeaderJson").val();
        var rptCfgInput = $("#rptCfgInput").val();
        if(rptHeaderJson == '') {
            alert("请先上传报表表头文件");
            return;
        }
        if(rptCfgInput == '') {
            alert("请选择报表配置文件");
            return;
        }

        this.showMask();
        $("#rptForm").ajaxSubmit({
            url: basePath + "/rptTools/saveRpt/importRptCfg",
            type: 'POST',
            dataType: "json",
            success: function (data) {
                $("#mask").hide();
                var status = data.status;
                if (status == '0') {
                    $("#preViewSQL").hide();
                    alert("文件上传失败,只能上传xml文件");
                } else {
                    $("#xmlCheck").val("1");
                    alert("文件上传成功");
                    $("#preViewSQL").show();
                }
            },
            error: function (data) {
                $("#preViewSQL").hide();
                alert(data);
            }
        });
    },

    /**
     * 下载报表模板
     */
    downLoadRptCfg : function () {
        var rptHeaderJson = $("#rptHeaderJson").val();
        var rptName = $("#rptName").val();

        if(rptName == '') {
            alert("请输入报表名称");
            return;
        }

        $("#downloadform").remove();
        var form = $("<form>");//定义一个form表单
        form.attr("id", "downloadform");
        form.attr("style", "display:none");
        form.attr("target", "");
        form.attr("method", "post");
        form.attr("action", basePath + "/rptTools/saveRpt/downLoadRptTmpleat");
        var input = $("<input>");
        input.attr("type", "hidden");
        input.attr("name", "rptHeaderInfo");
        input.attr("id", "rptHeaderInfo");
        input.attr("value", rptHeaderJson);
        form.append(input);

        var input1 = $("<input>");
        input1.attr("type", "hidden");
        input1.attr("name", "rptNameInfo");
        input1.attr("id", "rptNameInfo");
        input1.attr("value", rptName);
        form.append(input);
        form.append(input1);
        $("body").append(form);//将表单放置在web中

        form.submit();//表单提交
    },

    /**
     * 预览报表SQL
     */
    preViewSQL : function () {
        var rptCfgInput = $("#rptCfgInput").val();
        var dbSource = $("#dbSource").val();

        if(rptCfgInput == '') {
            alert("请上传报表配置文件");
            return;
        }
        if(dbSource == '') {
            alert("请选择数据源");
            return;
        }

        $("#rptForm").ajaxSubmit({
            url:basePath + "/rptTools/saveRpt/preViewSQL",
            type:'POST',
            dataType:'json',
            data:{
                "dbSource": dbSource
            },
            async: false,
            success:function(data){
                var status = data.status;
                var querySQL = data.querySQL;
                var errorMsg = data.errorMsg;
                if(status == "1") {
                    layer.open({
                        type: 1,
                        title: '报表预览SQL',
                        skin: 'layui-layer-rim', //加上边框
                        area: ['550px', '370px'], //宽高
                        content: querySQL
                    });
                } else {
                    alert(errorMsg);
                }
            }
        })
    },

    /**
     * 生成报表
     */
    saveRpt : function () {
        var rptName = $("#rptName").val();
        var dbSource = $("#dbSource").val();
        var accessIn = $("#accessIn").val();
        var rptHeaderJson = $("#rptHeaderJson").val();
        var rptCfgInput = $("#rptCfgInput").val();
        var xmlCheck = $("#xmlCheck").val();

        if(rptName == '') {
            alert("请输入报表名称");
            return;
        }
        if(dbSource == '') {
            alert("请选择数据源");
            return;
        }
        if(rptHeaderJson == '') {
            alert("请上传报表表头文件");
            return;
        }
        if(rptCfgInput == '') {
            alert("请上传报表配置文件");
            return;
        }
        if(xmlCheck != '1') {
            alert("报表配置文件只能是XML文件");
            return;
        }

        this.showMask();
        $("#rptForm").ajaxSubmit({
            url: basePath + "/rptTools/saveRpt/saveRptCfg",
            type: 'POST',
            data:{
                "rptName" : rptName,
                "dbSource": dbSource,
                "accessIn": accessIn,
                "rptHeaderJson" : rptHeaderJson
            },
            dataType: "json",
            success: function (data) {
                $("#mask").hide();
                var status = data.status;
                if (status == '0') {
                    var errorMsg = data.errorMsg;
                    alert(errorMsg);
                } else {
                    alert("生成报表成功");
                }
            },
            error: function (data) {
                alert(data);
            }
        });
    },

    showMask : function () {
        $("#mask").css("height",$(document).height());
        $("#mask").css("width",$(document).width());
        $("#mask").text("正在上传...");
        $("#mask").show();
    }
}
